#include "os.h"

#include <unistd.h>
#include <utime.h>
#include <time.h>

#include "exception.h"


namespace evm { namespace os {

uid_t getuid(void) {
    return ::getuid();
}

uid_t geteuid(void) {
    return ::geteuid();
}

void chown(const string& path, uid_t owner, gid_t group) {
    check_unix_error(::chown(path.c_str(), owner, group), "chown");
}

void utime(const string& path) {
    time_t now;
    ::time(&now);
    utimbuf times = {now, now};
    check_unix_error(::utime(path.c_str(), &times), "utime");
}

stat_type stat(const string& path) {
    stat_type s;
    check_unix_error(::stat(path.c_str(), &s), "stat");
    return s;
}

double mtime(const string& path) {
    timespec ts = stat(path).st_mtim;
    return ts.tv_sec + ts.tv_nsec * 1e-9;
}

double atime(const string& path) {
    timespec ts = stat(path).st_atim;
    return ts.tv_sec + ts.tv_nsec * 1e-9;
}

double ctime(const string& path) {
    timespec ts = stat(path).st_ctim;
    return ts.tv_sec + ts.tv_nsec * 1e-9;
}

}}
